#include "clientdatabase.h"
#include <QDebug>

clientDataBase *clientDataBase::self=nullptr;

clientDataBase::clientDataBase()
{



  cdatabase=QSqlDatabase::addDatabase("QSQLITE","local_chat_record");
  cdatabase.setDatabaseName("./pass_account.db");
  if (cdatabase.isValid()) {
      qDebug() << "成功设置数据库名称！";
  } else {
      qDebug() << "设置数据库名称失败！";
  }
  if (!cdatabase.open()) {
      qDebug() << "Failed to open database";
      return;
  }
   QSqlQuery query(cdatabase);

   QString s="create table group_chat (username varchar,message varchar);";
   if(IsTableExist("group_chat")){
       qDebug()<<s<<"表已存在，不需要再创建新的"<<endl;
   }
   else {
       if(query.exec(s))
       {
           qDebug()<<"创建表成功"<<endl;
       }
       else
           qDebug()<<"创建表失败"<<endl;
   }
}

void clientDataBase::create_new_table(const QString &tab){
     QSqlQuery query(cdatabase);
     QString s= QString ("create table '%1' (useraccount varchar,username varchar,message varchar);").arg(tab);
   // QString s="create table "+tab+"(useraccount varchar,username varchar,message varchar);";
    if(IsTableExist(tab)){
        qDebug()<<s<<"表已存在，不需要再创建新的"<<endl;
    }
    else {
        if(query.exec(s))
        {
            qDebug()<<"创建表成功"<<endl;
        }
        else
            qDebug()<<"创建表失败"<<endl;
    }
}

bool clientDataBase::IsTableExist(const QString &tab){
    QSqlQuery query(cdatabase);
    QString sq1=QString("select * from sqlite_master where type='table' and name ='%1';").arg(tab);
    query.exec(sq1);
    if(query.first()){
        qDebug()<<"tab"<<tab<<"存在"<<endl;
        return true;
    }
    else
    {
        qDebug()<<"tab"<<tab<<"不存在"<<endl;
        return false;
    }
}

void clientDataBase::add_groupchat_record(const QString &tab, const QString &username, const QString &message){
     QSqlQuery query(cdatabase);
     if(!IsTableExist(tab)){
         create_new_table(tab);
     }
     QString s=QString("insert into '%1' (username,message) VALUES('%2','%3');").arg(tab,username,message);
     if(query.exec(s))
     {
         qDebug()<<" 执行插入操作"<<endl;
     }
     else
         qDebug()<<"未执行插入操作"<<endl;

}

void clientDataBase::add_singlechat_record(const QString &tab, const QString &useracc, const QString &username, const QString &message){
    QSqlQuery query(cdatabase);
    if(!IsTableExist(tab)){
        create_new_table(tab);
    }
    QString s=QString("insert into '%1' (useraccount,username,message) VALUES('%2','%3','%4');").arg(tab,useracc,username,message);
    if(query.exec(s))
    {
        qDebug()<<" 执行插入操作"<<endl;
    }
    else
        qDebug()<<"未执行插入操作"<<endl;
}

void clientDataBase::clear_singlechat_record(const QString &tab,const QString&friacc){
    QSqlQuery query(cdatabase);
    QString s=QString("delete from '%1' where useraccount='%2'").arg(tab,friacc);
    if(query.exec(s))
    {
        qDebug()<<" 执行清空操作"<<endl;
    }
    else
        qDebug()<<"未执行清空操作"<<endl;
}
void clientDataBase::clear_groupchat_record(){
    QSqlQuery query(cdatabase);
    QString s=QString("delete from group_chat");
    query.exec(s);
}

void clientDataBase::show_group_chatrecord(QVector<QString> &showrecord){
       QSqlQuery query(cdatabase);
    QString s="SELECT * FROM group_chat;";
    query.exec(s);
    while(query.next()){
        QString s=query.value("username").toString()+":"+query.value("message").toString();
        showrecord.push_back(s);
    }
}

void clientDataBase::show_single_chatrecord(QVector<QString> &showrecord, const QString &tab, const QString &friaccount){
    QSqlQuery query(cdatabase);
    QString s=QString("SELECT * FROM '%1' WHERE useraccount='%2';").arg(tab,friaccount);
    query.exec(s);
    while(query.next()){
        QString s=query.value("username").toString()+":"+query.value("message").toString();
        showrecord.push_back(s);
    }
}
